# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=intiface-central
pkgver=2.5.1
pkgrel=1
pkgdesc="Frontend application for the Buttplug Sex Toy Control Library"
url="https://intiface.com/central/"
arch="aarch64 x86_64"	# flutter
license="GPL-3.0-only"
makedepends="
	cargo
	corrosion
	dbus-dev
	eudev-dev
	flutter-desktop
	openssl-dev
	patchelf
	"
options="net !check"	# no tests
source="
	https://github.com/intiface/intiface-central/archive/refs/tags/v$pkgver/intiface-central-$pkgver.tar.gz

	system-corrosion.patch
	system-flutter.patch
	pmos-fit-screen.patch
	no-werror.patch
	flutter-3.16.patch

	intiface-central
	intiface-central.desktop
	"

case "$CARCH" in
	x86_64) _flutter_arch="x64" ;;
	aarch64) _flutter_arch="arm64" ;;
esac

prepare() {
	default_prepare

	flutter pub get --enforce-lockfile

	(
		cd intiface-engine-flutter-bridge

		cargo fetch --target="$CTARGET" --locked
	)
}

build() {
	flutter build linux --release

	for _elf in build/linux/$_flutter_arch/release/bundle/lib/lib*.so; do
		# running patchelf on libapp (the AOT snapshot of the whole Dart codebase)
		# breaks it with "[FATAL:flutter/runtime/dart_vm_initializer.cc(89)]
		# Error while initializing the Dart VM: Invalid vm isolate snapshot seen".
		#
		# it has no rpath and only links to libc soname anyway. 🤷‍♀️
		[ "$(basename "$_elf")" != "libapp.so" ] || continue

		patchelf --set-rpath '$ORIGIN' "$_elf"
	done
}

package() {
	local bundle="$builddir"/build/linux/$_flutter_arch/release/bundle

	install -Dm755 "$bundle"/${pkgname/-/_} "$pkgdir"/usr/lib/$pkgname/${pkgname/-/_}
	cp -r "$bundle"/lib "$pkgdir"/usr/lib/$pkgname/lib
	cp -r "$bundle"/data "$pkgdir"/usr/lib/$pkgname/data

	install -Dm755 "$srcdir"/$pkgname "$pkgdir"/usr/bin/$pkgname

	install -Dm644 "$srcdir"/$pkgname.desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	install -Dm644 assets/icons/intiface_central_icon.png "$pkgdir"/usr/share/icons/hicolor/512x512/apps/$pkgname.png
}

sha512sums="
5561d1bc0fcba5a0b712a4bd8f1604cc845ba20b0064032c6a5bfae65ee287dd72b729858c9d1f53246df366a52b6821d3eaa897358574657d348b7d18df28a9  intiface-central-2.5.1.tar.gz
ecad3c5cee9c53fc88b539d3088907539aff0b28980a89d4be2acb3c329a7a2154cb12743c0786e5c4a33de78af06b61b5b35f8eee633989cc943dc14e87dba6  system-corrosion.patch
91014708ab7bb81518ce93db0612209d6479dfa503064d9e9d3186cd5e861165a3f63e286d271d5aac8a1d676868fc3acf51648ac01c16a215062a317ccb6623  system-flutter.patch
9d408da594f5e67c98438c3f65600ecded29c6a5750f8d3d20799420444236cf11ae08321f344e389e126aa97111d20f8dcc27ef9d8aecfae967dcc87ffa6eb8  pmos-fit-screen.patch
1d2b7d28a09e26ed48d89aa5f5dffb738ba367fa2760b12980225239705e16fa1ec89901d7aecf26c8527de721a5f50f50cc77ea009cd5f25f219d1af8730ed8  no-werror.patch
2aef5073fe4df1e29ad12836bf95fe4c88425eabb8a52b063f7baada7f7b9f2e4472a2d28780a9ccfe1c2749b08fc266cc8f2898520202465335005325c2b4d8  flutter-3.16.patch
a69d49b59ebd0dbeeace74954d72cd97c38aa79597887648f0c8f5ec4ecb191a1b47c43d3007e7c826323800a601951e621ee60ed739f17f67fca2f8855d95b0  intiface-central
4015b388f997eb3abc2ecd69947bb703926e01de82807a940a42902f46e90d6759f609921215ba865cb6c91f5c6d404115c16a0a7ebe4bcc35315a4910adc081  intiface-central.desktop
"
